#include "printer.h"
#include "qdatetime.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "qpushbutton.h"
#include <QPlainTextEdit>
#include <QToolButton>
Printer *Printer::init()
{
    static Printer _logger;
    return &_logger;
}

void Printer::print(QTextEdit *ob, QString s, Level level)
{
    if(level<m_type){
        return;
    }
    ob->append(getMesg(level,s));
}

void Printer::log(void* eq, QString msg, Level type, EquipmentType equ)
{
    if(type<m_type){
        return;
    }
    msg=getMesg(type,msg);
    switch (equ) {
    case Console:
        qDebug()<<msg;
        break;
    case Label:
        ((QLabel*)eq)->setText(msg+"\n");
        break;
    case TextEdit:
        ((QTextEdit*)eq)->append(msg+"\n");
        break;
    case LineEdit:
        ((QLineEdit*)eq)->setText(msg);
        break;
    case PlainTextEdit:
        ((QPlainTextEdit*)eq)->appendPlainText(msg+"\n");
        break;
    case ToolButton:
        ((QToolButton*)eq)->setText(msg);
        break;
    case PushButton:
        ((QPushButton*)eq)->setText(msg);
        break;
    }
}

void Printer::log(QString s){
    qDebug()<<s;
}

QString Printer::levelToString(Printer::Level level){

    switch (level)
    {
    case Printer::L_DEBUG:
        return QString("[DEBUG] ");
    case Printer::L_INFO:
        return QString("[INFO] ");
    case Printer::L_WARNING:
        return QString("[WARNING] ");
    case Printer::L_ERROR:
        return QString("[ERROR] ");
    case Printer::L_CRITICAL:
        return QString("[CRITICAL] ");
    default:
        break;
    }
    return QString();
}


QString Printer::getMesg(Level level, QString &text){
    QString msg=levelToString(level);
    msg += " ";
    QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
    QString time = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
    msg += (std::move(time) + ": \"" + text + "\"\n");
    return msg;
}
